package com.ahuiapp.ahuibang;

import java.util.ArrayList;

import com.ahuiapp.ahuibang.MainActivity.SiriListItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


/**消息显示Adapter
 * @author Administrator
 * 用于将信息显示到list上，同时设置背景为什么样式
 */
public class MsgViewAdapter extends BaseAdapter {
	private ArrayList<SiriListItem> list;
	private Context ctx;
	private LayoutInflater mInflater;

	public MsgViewAdapter(Context context, ArrayList<SiriListItem> l) {
		ctx = context;
		list = l;
		mInflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		SiriListItem  item=list.get(position);
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.list_item, null);          
			viewHolder=new ViewHolder(
					(View) convertView.findViewById(R.id.list_child),
					(TextView) convertView.findViewById(R.id.chat_msg)
			);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}    
		if(item.isfromNet){
			viewHolder.child.setBackgroundResource(R.drawable.msgbox_send);//child.setBackgroundResource(R.drawable.msgbox_rec);
//			viewHolder.child.setPadding(0, 0, 100, 0);
			
		}else {
			//viewHolder.child.setBackgroundResource(R.drawable.msgbox_send);
			viewHolder.child.setBackgroundResource(R.drawable.msgbox_rec);
//			viewHolder.child.setPadding(100, 0, 0, 0);
		}
		viewHolder.msg.setText(item.message);
		return convertView;
	}

	class ViewHolder {
		protected View child;
		protected TextView msg;
		public ViewHolder(View child, TextView msg){
			this.child = child;
			this.msg = msg;
		}
	}
}
